﻿using System.Web.UI;

namespace Guru.Web.Controls.Validators
{
    /// <summary>
    /// A server control's view state comprises the cumulative values of the control's properties. 
    /// This interface includes methods that save and load a server control's view state values,
    /// as well as a method that instructs the control to track any changes to its view state.
    /// To customize the way your ASP.NET application manages server control view state,
    /// you must create a class that implements this interface since you cannot inherit
    /// from the StateBag class.
    /// </summary>
    public class StateManager : IStateManager
    {
        #region Members
        private bool _isTrackingViewState;
        private StateBag _viewState;
        #endregion


        #region IStateManager implementation
      
        /// <summary>
        /// The StateBag object that allows you to save
        /// and restore view-state information
        /// </summary>
        protected virtual StateBag ViewState
        {
            get
            {
                if (_viewState == null)
                {
                    _viewState = new StateBag(false);

                    if (_isTrackingViewState)
                    {
                        ((IStateManager)_viewState).TrackViewState();
                    }
                }
                return _viewState;
            }
        }

        /// <summary>
        /// Implement the IsTrackingViewState method for this class 
        /// by calling the IsTrackingViewState method of the class's
        /// private _viewstate property. 
        /// </summary>
        bool IStateManager.IsTrackingViewState
        {
            get
            {
                return _isTrackingViewState;
            }
        }

        /// <summary>
        /// Implement the LoadViewState method. If the saved view state
        /// exists, the view-state value is loaded to the ValidationItem control
        /// </summary>
        /// <param name="savedState"></param>
        void IStateManager.LoadViewState(object savedState)
        {
            if (savedState != null)
            {
                ((IStateManager)ViewState).LoadViewState(savedState);
            }
        }

        /// <summary>
        /// Implement the SaveViewState method. If the StateBag
        ///  that stores the MyItem class's view state contains
        ///  a value for the message property and if the value
        ///  has changed since the TrackViewState method was last 
        ///  called, all view state for this class is deleted, 
        ///  using the StateBag.Clear method,and the new value is added.
        /// </summary>
        /// <returns></returns>
        object IStateManager.SaveViewState()
        {
            object savedState = null;

            if (_viewState != null)
            {
                savedState = ((IStateManager)_viewState).SaveViewState();
            }
            return savedState;
        }

        /// <summary>
        ///  Implement the TrackViewState method for this class by
        /// calling the TrackViewState method of the class's private
        /// _viewstate property.
        /// </summary>
        void IStateManager.TrackViewState()
        {
            _isTrackingViewState = true;

            if (_viewState != null)
            {
                ((IStateManager)_viewState).TrackViewState();
            }
        }

        internal void SetDirty()
        {
            _viewState.SetDirty(true);
        }
        #endregion
    }
}
